//
//  DynamicPicturesView.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-10.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "DynamicPicturesView.h"
#import "Config.h"
#import "ASImageView.h"

@implementation DynamicPicturesView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _firstImageView = [[ASImageView alloc] initWithFrame:CGRectMake(0, 0, _PICTURE_NORMAL_PREVIEW_WIDTH, _PICTURE_NORMAL_PREVIEW_HEIGHT)];
        _firstImageView.backgroundColor = [UIColor clearColor];
//        [_firstImageView setBorderWithColor:[UIColor whiteColor] andWidth:1];
        [self addSubview:_firstImageView];
        
        _secondImageView = [[ASImageView alloc] initWithFrame:CGRectMake(_firstImageView.frame.size.width + _firstImageView.frame.origin.x + 13, 0, _PICTURE_NORMAL_PREVIEW_WIDTH, _PICTURE_NORMAL_PREVIEW_HEIGHT)];
        _secondImageView.backgroundColor = [UIColor clearColor];
//        [_secondImageView setBorderWithColor:[UIColor whiteColor] andWidth:1];
        [self addSubview:_secondImageView];
        
        _thirdImageView = [[ASImageView alloc] initWithFrame:CGRectMake(_secondImageView.frame.size.width + _secondImageView.frame.origin.x + 13, 0, _PICTURE_NORMAL_PREVIEW_WIDTH, _PICTURE_NORMAL_PREVIEW_HEIGHT)];
        _thirdImageView.backgroundColor = [UIColor clearColor];
//        [_thirdImageView setBorderWithColor:[UIColor whiteColor] andWidth:1];
        [self addSubview:_thirdImageView];
    }
    return self;
}


// 重新绘制图片子视图
- (void)drawPictures:(NSArray *)pictures
{
    //重置
    self.hidden = true;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    _firstImageView.image  = nil;
    _secondImageView.image = nil;
    _thirdImageView.image  = nil;

    if (pictures != nil && pictures > 0) {
        //设置为显示
        self.hidden = false;
        //并且高度有53
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _PICTURE_NORMAL_PREVIEW_HEIGHT);
        
        //遍历图片数组，并将图片绘制到图片视图中对应位置去
        for (int i = 0; i < pictures.count; i++) {

            NSURL* previewURL = [NSURL URLWithString:[[pictures objectAtIndex:i] objectForKey:@"previewUrl"]];
            NSURL* URL = [NSURL URLWithString:[[pictures objectAtIndex:i] objectForKey:@"url"]];
            
            if (i == 0) {
                [_firstImageView setImageURL:previewURL andSourceImageURL:URL forStyle:PictureStyleNormalPreview];
            }
            else if (i == 1) {
                [_secondImageView setImageURL:previewURL andSourceImageURL:URL forStyle:PictureStyleNormalPreview];
            }
            else if (i == 2) {
                [_thirdImageView setImageURL:previewURL andSourceImageURL:URL forStyle:PictureStyleNormalPreview];
            }
        }
    }
}

@end